﻿using Romantics.Domain.Objects;
using Romantics.Models;
using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Romantics
{
    public static class ActionLinkExtensions
    {
        public static MvcHtmlString CategoryLink(this HtmlHelper helper, Category category, string controller = "Hike", Category selected = null)
        {
            if (category == selected)
            {
                UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                StringBuilder builter = new StringBuilder("<a href=\"");
                builter.Append(urlHelper.Action("category", controller, new { category_url = category.URL, page = "" }));
                builter.Append("\" title = \"" + String.Format("Посмотреть все походы в категории \"{0}\"", category.Name)+"\">");
                builter.Append("<span class=\"active\">" + category.Name +"</span></a>");
                return new MvcHtmlString(builter.ToString());
            }
            return helper.ActionLink(category.Name, "category", controller
                , new { category_url = category.URL, page = "" }
                , new { title = String.Format("Посмотреть все походы в категории \"{0}\"", category.Name)}
                );
        }

        public static MvcHtmlString HikeLink(this HtmlHelper helper, Hike hike)
        {
            return helper.ActionLink(hike.Name, "Hike", "Hike"
                , new { id = hike.ID }
                , new { title = hike.Name }
                );
        }

        public static MvcHtmlString ArchiveLink(this HtmlHelper helper, Hike hike)
        {
            return helper.ActionLink(hike.Name, "Archive", "Archive"
                , new { id = hike.ID }
                , new { title = hike.Name }
                );
        }

        public static MvcHtmlString RegionLink(this HtmlHelper helper, Region region, string controller = "Hike")
        {
            return helper.ActionLink(String.Format("{0}. {1}", region.Country.Name, region.Name), "Region", controller
                , new { region_url = region.URL, page = "" }
                , new { title = String.Format("Посмотреть все походы в региону \"{0}.{1}\"",region.Country.Name, region.Name)}
                );
        }

        public static MvcHtmlString ShortRegionLink(this HtmlHelper helper, Region region, string controller = "Hike", Region selected = null)
        {
            if (region == selected)
            {
                UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                StringBuilder builter = new StringBuilder("<a href=\"");
                builter.Append(urlHelper.Action("Region", controller, new { region_url = region.URL, page = "" }));
                builter.Append("\" title = \"" + String.Format("Посмотреть все походы в региону \"{0}.{1}\"", region.Country.Name, region.Name) + "\">");
                builter.Append("<span class=\"active\">" + String.Format("{0}", region.Name) + "</span></a>");
                return new MvcHtmlString(builter.ToString());
            }
            return helper.ActionLink(String.Format("{0}", region.Name), "Region", controller
                , new { region_url = region.URL, page = "" }
                , new { title = String.Format("Посмотреть все походы в региону \"{0}.{1}\"", region.Country.Name, region.Name) }
                );
        }

        public static MvcHtmlString YearLink(this HtmlHelper helper, int year, string controller = "Archive", string action = "Index", string url = "", int? selected = null)
        {
            if (selected.HasValue && year == selected.Value)
            {
                UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                StringBuilder builter = new StringBuilder("<a href=\"");
                builter.Append(urlHelper.Action(action, controller, new { year = year, url = url, page=1 }));
                builter.Append("\" title = \"" + String.Format("Посмотреть походы за {0} год", year) + "\">");
                builter.Append("<span class=\"active\">" + year.ToString() + "</span></a>");
                return new MvcHtmlString(builter.ToString());
            }
            return helper.ActionLink(year.ToString(), action, controller, new { year = year, url = url, page=1 }, new { title = String.Format("Посмотреть походы за {0} год", year) });
        }

        public static MvcHtmlString PhotoAlbumLink(this HtmlHelper helper, PhotoAlbum album)
        {
            return helper.ActionLink(album.Name, "PhotoAlbum", "PhotoAlbum"
                , new { id = album.ID, page = "" }
                , new { title = String.Format("Посмотреть все фотографии в альбоме \"{0}\"", album.Name) }
                );
        }

        public static MvcHtmlString ReportLink(this HtmlHelper helper, Report report)
        {
            return helper.ActionLink(report.Name, "Report", "Archive"
                , new { id = report.ID }
                , new { title = String.Format("Посмотреть отчет \"{0}\"", report.Name) }
                );
        }

        private static string PageLinkWrapper(string href, string innerHtml)
        {
            TagBuilder tag = new TagBuilder("a");
            if (href != null)
            {
                tag.MergeAttribute("href", href);
                tag.InnerHtml = innerHtml;
            }
            else
            {
                TagBuilder span = new TagBuilder("span");
                span.AddCssClass("active");
                span.InnerHtml = innerHtml;
                tag.InnerHtml = span.ToString();
            }
            TagBuilder div = new TagBuilder("div");
            div.AddCssClass("tags-div");
            div.InnerHtml = tag.ToString();
            return div.ToString();
        }

        public static string Href(this NamedEntity entity, UrlHelper helper)
        {
            return helper.RouteUrl(new { controller = entity.GetType().Name, action = entity.GetType().Name, id = entity.ID });
        }

        public static string Href(this PhotoAlbumItem entity, UrlHelper helper)
        {
            return helper.RouteUrl(new { controller = "Photo", action = "Index", id = entity.ID });
        }

        public static MvcHtmlString PageLinks(
            this HtmlHelper html,
            HikeViewModel pagingInfo,
            Func<int, string> pageUrl
            )
        {
            if (pagingInfo.TotalPages <= 1)
                return new MvcHtmlString(String.Empty);
            StringBuilder result = new StringBuilder();
            int start = pagingInfo.CurrentPage - 4;
            if (start <= 0)
                start = 1;
            int finish = start + 8;
            if (finish > pagingInfo.TotalPages)
                finish = pagingInfo.TotalPages;
            if (pagingInfo.CurrentPage > 1)
            {
                if (start > 1)
                    result.Append(PageLinkWrapper(pageUrl(1), "<< Первая"));
                if (pagingInfo.CurrentPage <= pagingInfo.TotalPages)
                    result.Append(PageLinkWrapper(pageUrl(pagingInfo.CurrentPage - 1), "< Предыдущая"));
            }
            for (int i = start; i <= finish; i++)
            {
                result.Append(PageLinkWrapper((i != pagingInfo.CurrentPage) ?pageUrl(i):null, i.ToString()));
            }
            if (pagingInfo.CurrentPage < pagingInfo.TotalPages)
            {
                result.Append(PageLinkWrapper(pageUrl(pagingInfo.CurrentPage + 1), "Следующая >"));
                if (finish < pagingInfo.TotalPages)
                    result.Append(PageLinkWrapper(pageUrl(pagingInfo.TotalPages), "Последняя >>"));
            }
            if (pagingInfo.CurrentPage > pagingInfo.TotalPages && pagingInfo.TotalPages > 0)
            {
                result.Append(PageLinkWrapper(pageUrl(pagingInfo.TotalPages), "Последняя >>"));
            }
            return MvcHtmlString.Create(result.ToString());
        }
    }
}